package com.hu.lsp.config.master;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author lsp
 * @Date 2020/5/13 14:44
 * @Version 1.0
 */
@Configuration
@MapperScan(value = "com.hu.lsp.dao.master",sqlSessionTemplateRef = "masterSqlSessionTemplate",sqlSessionFactoryRef  = "masterSqlSessionFactory")
public class MasterDatasourceConfig {
    private static final Logger log = LoggerFactory.getLogger(MasterDatasourceConfig.class);

    /**
     * 创建DataSource
     * @return
     */
    @Primary
    @Bean("masterDataSource")
  //  @ConfigurationProperties(prefix = "spring.datasource.druid.master")
    public DataSource masterDataSource(MasterConfig masterConfig) throws SQLException {
        log.info("masterDateSoure{}",masterConfig);
        MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();
        mysqlXaDataSource.setUrl(masterConfig.getUrl());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXaDataSource.setPassword(masterConfig.getPassword());
        mysqlXaDataSource.setUser(masterConfig.getUsername());
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);
        //注册到全局事务
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXaDataSource);
        xaDataSource.setUniqueResourceName(masterConfig.getUniqueResourceName());
        xaDataSource.setMinPoolSize(masterConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(masterConfig.getMaxPoolSize());
        xaDataSource.setMaxLifetime(masterConfig.getMaxLifetime());
        xaDataSource.setBorrowConnectionTimeout(masterConfig.getBorrowConnectionTimeout());
        xaDataSource.setLoginTimeout(masterConfig.getLoginTimeout());
        xaDataSource.setMaintenanceInterval(masterConfig.getMaintenanceInterval());
        xaDataSource.setMaxIdleTime(masterConfig.getMaxIdleTime());
        xaDataSource.setTestQuery(masterConfig.getTestQuery());
        return xaDataSource;
    }

    /**
     * 创建sqlSessionFactory
     * @param dataSource
     * @return
     * @throws Exception
     */
    @Primary

    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
      //  SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //分页插件
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.addInterceptor(new PaginationInterceptor());
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*Mapper.xml"));
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean.getObject();
    }

    /**
     * 创建事务管理器
     * @param dataSource
     * @return
     */
  /*  @Primary
    @Bean(name = "masterTransactionManager")
    public DataSourceTransactionManager masterTransactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }*/
    /**
     * 创建sqlSession模板
     * @param sqlSessionFactory
     * @return
     */
    @Primary

    @Bean(name = "masterSqlSessionTemplate")

    public SqlSessionTemplate masterSqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}